#include "file.h"
using namespace std;

File::File()
{}

// Read file contents
QString File::read(QString filename){
    QString content = "";
    QFile file(filename);
    // 打开文件
    if (file.open(QIODevice::ReadOnly | QIODevice::Text)){
        QTextStream in(&file);
        // 读取内容（直至结束）
        while (!in.atEnd()) {
            QString line = in.readLine() + "\n";
            content += line;
        }
        //content = in.readAll();
    }
    // qDebug()<<content;
    return content;
}

// Write String Content To File
void File::write(QString filename, QString content){
    QFile file(filename);
    if (file.open(QIODevice::WriteOnly | QIODevice::Text)){
        QTextStream out(&file);
        out << content;
    }
}

// Return Users Home Directory
QString File::getHomeDir(){
    QString homeDir = QDir::homePath();
    return homeDir;
}

QString File::getDirectory(QString filepath){
    // 移除文件名 获取文件的上一级
    int lastIndex = filepath.lastIndexOf("/");
    filepath.chop(filepath.length() - lastIndex);

    return filepath;
}
